package com.lzy.webapi.controller;

import com.lzy.framework.core.ensure.Ensure;
import com.lzy.framework.core.output.Result;
import com.lzy.framework.core.utils.DozerUtils;
import com.lzy.framework.tools.commons.BigDecimalUtil;
import com.lzy.product.apis.AgentCommissionBillServiceApi;
import com.lzy.product.apis.MemberCouponsServiceApi;
import com.lzy.product.constants.BusinessType;
import com.lzy.product.dtos.MemberCouponsDto;
import com.lzy.product.dtos.agent.AgentCommissionBillDto;
import com.lzy.webapi.component.UserToken;
import com.lzy.webapi.facade.MemberCouponsFacade;
import com.lzy.webapi.response.AgentSaleResponse;
import com.lzy.webapi.response.MemberCouponsResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import java.math.BigDecimal;

/**
 * 卡券宝服务
 * Created by WIN7 on 2018/1/6.
 */
@Controller
@RequestMapping("api/coupons")
public class MemberCouponsController extends BaseController{

    @Autowired
    private MemberCouponsServiceApi memberCouponsServiceApi;

    @Autowired
    private MemberCouponsFacade memberCouponsFacade;

    @Autowired
    private AgentCommissionBillServiceApi commissionBillServiceApi;

    /**
     * 卡券宝详情
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getMemberCoupons", method = RequestMethod.GET)
    public @ResponseBody
    Result getMemberCoupons(@RequestHeader("token") String token) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        MemberCouponsDto dto = memberCouponsServiceApi.findByMemberIdAndType(ut.getUserId(), BusinessType.MemberCouponsType.PROFIT.getCode());
        MemberCouponsResponse response = new MemberCouponsResponse("0", "0");
        if(dto != null) {
            response = DozerUtils.map(dto, MemberCouponsResponse.class);
        }
        memberCouponsFacade.setParamValues(response, ut.getUserId());
        return new Result(response);
    }


    /**
     * 代理商销售详情
     *
     * @param token
     * @return
     */
    @RequestMapping(value = "/getAgentProfit", method = RequestMethod.GET)
    public @ResponseBody
    Result getAgentProfit(@RequestHeader("token") String token) {
        UserToken ut = isLogin(token);
        Ensure.that(ut).isNotNull("01000006");
        AgentSaleResponse response = new AgentSaleResponse();
        AgentCommissionBillDto commissionBillDto = new AgentCommissionBillDto();
        commissionBillDto.setMemberId(ut.getUserId());
        BigDecimal allCommAmount = commissionBillServiceApi.getCommAmount(commissionBillDto);
        if(BigDecimalUtil.isZeroOrNull(allCommAmount)){
            response.setAllCommAmount(allCommAmount);
        }
        MemberCouponsDto dto = memberCouponsServiceApi.findByMemberIdAndType(ut.getUserId(), BusinessType.MemberCouponsType.AGENTPROFIT.getCode());
        if(dto != null) {
            if(BigDecimalUtil.isZeroOrNull(dto.getAvailableMoney())){
                response.setAvailableMoney(dto.getAvailableMoney());
            }
        }
//        String systemName = memberCouponsFacade.getSystemName(ut.getUserId());
//        if(systemName != ""){
//            response.setSystemName(systemName);
//        }
        response.setSystemName("代理商");
        return new Result(response);
    }
}
